package com.epam.diplom.bean;


import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;


@Entity
@Table(name = "CLIENT")
@NamedQueries(value = {
		@NamedQuery(name = "checkClient",query = "SELECT COUNT(*) FROM Client c WHERE " +
				"c.firstName = ? AND c.lastName = ? AND c.patronymik = ? AND c.phone = ? " +
				"AND c.email = ?")
})
public class Client implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "CLIENTID")
	private long id;
	
	@NotEmpty
	@Length(max = 30)
	@Column(name = "FIRSTNAME")
	private String firstName;

	@NotEmpty
	@Length(max = 30)
	@Column(name = "LASTNAME")
	private String lastName;
	
	@NotEmpty
	@Length(max = 30)
	@Column(name = "PATRONYMIK")
	private String patronymik;
	
	@Email
	@Length(max = 40)
	@Column(name = "EMAIL")
	private String email;
	
	@Pattern(regexp = "[(]+\\d{3}[)]+[\\s]+\\d{3}[-]+\\d{4}")
	@Column(name = "PHONE")
	@Length(max = 15)
	private String phone;
	
	public Client() {		
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPatronymik() {
		return patronymik;
	}

	public void setPatronymik(String patronymik) {
		this.patronymik = patronymik;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Firstname: ").append(firstName);
		builder.append(", Lastname: ").append(lastName);
		builder.append(", Patronymik: ").append(patronymik);
		builder.append(", Phone: ").append(phone);
		builder.append(", E-mail: ").append(email);
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result
				+ ((patronymik == null) ? 0 : patronymik.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (patronymik == null) {
			if (other.patronymik != null)
				return false;
		} else if (!patronymik.equals(other.patronymik))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		return true;
	}
}
